package com.suineg.pms.entity.fee;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springside.modules.utils.Collections3;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.suineg.pms.entity.IdEntity;
import com.suineg.pms.entity.User;
import com.suineg.pms.entity.base.Room;

/**
 * 收费方案
 *@author Suine
 */
@Entity
@Table(name = "pms_fee_plan")
public class FeePlan extends IdEntity{
	/** 方案名称 */
	private String name;
	/** 生效时间 */
	private Date startDate;
	/** 失效时间 */
	private Date endDate;
	/** 是否生效 */
	private Long isEnable;
	/** 操作人 */
	private User opuser;
	/** 操作时间 */
	private Date opdate;
	/** 标志 1- 公共 ，非1 - 私人 */
	private Long flag;
	/** 房间 */
	private List<Room> rooms = Lists.newArrayList();
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "pms_plan_room",joinColumns={@JoinColumn(name = "plan_id")},inverseJoinColumns={@JoinColumn(name ="room_id")})
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序
	@OrderBy("id ASC")
	// 缓存策略
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Room> getRooms() {
		return rooms;
	}
	public void setRooms(List<Room> rooms) {
		this.rooms = rooms;
	}
	public FeePlan() {
	}
	public FeePlan(String name, Date startDate, Date endDate, Long isEnable, User opuser, Date opdate) {
		super();
		this.name = name;
		this.startDate = startDate;
		this.endDate = endDate;
		this.isEnable = isEnable;
		this.opuser = opuser;
		this.opdate = opdate;
	}
	private List<FeeScal> feeScalList = Lists.newArrayList();
	
	/** 方案与基准中间实体 */
	private Set<PlanScal> planScal = Sets.newHashSet();
	@OneToMany(mappedBy = "feePlan",fetch=FetchType.LAZY)
	public Set<PlanScal> getPlanScal() {
		return planScal;
	}
	
	public void setPlanScal(Set<PlanScal> planScal) {
		this.planScal = planScal;
	}
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name="pms_plan_scale",joinColumns={@JoinColumn(name = "plan_id")},inverseJoinColumns={@JoinColumn(name = "scale_id")})
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序
	@OrderBy("id ASC")
	// 缓存策略
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<FeeScal> getFeeScalList() {
		return feeScalList;
	}
	public void setFeeScalList(List<FeeScal> feeScalList) {
		this.feeScalList = feeScalList;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	public Long getIsEnable() {
		return isEnable;
	}
	public void setIsEnable(Long isEnable) {
		this.isEnable = isEnable;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	public User getOpuser() {
		return opuser;
	}
	public void setOpuser(User opuser) {
		this.opuser = opuser;
	}
	public Date getOpdate() {
		return opdate;
	}
	public void setOpdate(Date opdate) {
		this.opdate = opdate;
	}
	public Long getFlag() {
		return flag;
	}
	public void setFlag(Long flag) {
		this.flag = flag;
	}
	@Transient
	public String getScalIds(){
		return  Collections3.extractToString(feeScalList, "id", ",");
	}
	
	/**
	 * 返回一个JSON串
	 * <pre>[{id:xx,cnt:x}]</pre>
	 * @return
	 */
	@Transient
	public String getSccalByJSON(){
		JSONArray array = new JSONArray();
		try {
			JSONObject obj = null;
			for(PlanScal ps : planScal){
				obj = new JSONObject();
				obj.put("id", ps.getFeeScal().getId());
				obj.put("cnt", ps.getCount() == null ? 0 : ps.getCount());
				obj.put("cycle", ps.getCycle());
				array.put(obj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return array.toString();
	}
	

}

